<?php
/**
 * Created by PhpStorm.
 * User: yzapple
 * Date: 2018/1/11
 * Time: 下午2:06
 */
namespace app\common\model;

use app\common\model\Base;

use think\Db;
class Patientcase extends Base
{
    protected $table = "patient_case";

    protected $base_arr = [
        'table' => 'patient_case',
        'field' => 'case_num,patient_id,doctor_id,check_time,check_result',
        'primary_key' => 'id'
    ];

    protected $attach_arr =  [
        [
            'table' => 'patient',
            'field' => 'name',
            'primary_key' => 'id',
            'base_table_field' => 'patient_id'
        ],
        [
            'table' => 'doctor',
            'field' => 'name',
            'primary_key' => 'id',
            'base_table_field' => 'doctor_id'
        ],

    ];

    //预计一维数组
    protected $senMsgAttach = [

                'table' => 'patient',
                'field' => 'phone',
                'primary_key' => 'id',
                'base_table_field' => 'patient_id'

        ];

    /**
     * 获取字段对应的数组
     * */
    public function getDataByMysql(){
        $data = [
            't_name' => $this->table,//必填
            'model' => '患者案例',//必填
            'case_num' => '病例编号',
            'patient_id' => '患者为',
            'doctor_id' => '录入医生',
            'check_time' => '检查时间',
            'check_result' => '检查结果',
            'patient_case_type' => '检查类型',
            'patient_part_lat' => '检查部位+经纬度',
            'electronic_report' => '电子报告',
            'electronic_film' => '电子胶片',
            'is_deleted' => '{"2":"功能禁用","1":"功能启用","3":"功能删除"}',
        ];
        return $data;
    }


    /**
     * 获取日志的访问方法
     **/
    public static function getPatientCaseAdminLog($list_action='',$form_data=[],$userId,$admin_type){
        $model = new Patientcase();
        $actionName = strtolower(ACTION_NAME);
        $controller = strtolower(CONTROLLER_NAME);
        $old_data = [];
        $type = '';

        switch($actionName){
            //列表
            case "index":
                Base::save_admin_log($userId, $model, 'check','',[],[],$admin_type);
                break;
            case "check_detail":
                $type = 'detail';
                $record_data['id'] = $form_data['id'];
                $form_data = DB::name($model->table)
                    ->field($model->base_arr['field'])
                    ->where($record_data)->find();
                $form_data['id'] = $record_data['id'];
                break;
            case "add_record":
                $type = 'add';
                if ($model->request->isPost()) {
                    unset($form_data['id']);
                    if(!isset($form_data['electronic_film'])){
                        return ['flag'=>'error','msg'=>'电子胶片部位不能为空','url'=>''];
                        exit;
                    }
                    if(!isset($form_data['electronic_report'])){
                        return ['flag'=>'error','msg'=>'电子报告不能为空','url'=>''];
                        exit;
                    }

                    foreach($form_data['electronic_film'] as $val){
                        if(empty($val['report_name'])){
                            return ['flag'=>'error','msg'=>'电子胶片部位不能为空','url'=>''];
                            exit;
                        //  return $model->error("电子胶片部位不能为空");
                        }
                        if(empty($val['img'])){
                            return ['flag'=>'error','msg'=>'电子胶片部位图片不能为空','url'=>''];
                            exit;
                           // return $model->error("电子胶片部位图片不能为空");
                        }
                    }

                    $form_data['patient_part_lat'] = json_encode($form_data['patient_part_lat']);
                    $form_data['electronic_report'] = json_encode($form_data['electronic_report']);
                    $form_data['electronic_film'] = json_encode($form_data['electronic_film']);
                }
                break;
            case "edit_record":
                $type = 'edit';
                if ($model->request->isPost()) {
                  //  print_r($form_data);
                    $old_data = Base::getOneOldDataByTables($form_data['id'],$model->base_arr,$model->attach_arr);
                    if(!isset($form_data['electronic_film'])){
                        return ['flag'=>'error','msg'=>'电子胶片部位不能为空','url'=>''];
                        exit;
                    }

                    if(!isset($form_data['electronic_report'])){
                        return ['flag'=>'error','msg'=>'电子报告不能为空','url'=>''];
                        exit;
                    }
                    foreach($form_data['electronic_film'] as $val){
                        if(empty($val['report_name'])){
                            return ['flag'=>'error','msg'=>'电子胶片部位不能为空','url'=>''];
                            exit;
                            //  return $model->error("电子胶片部位不能为空");
                        }
                        if(empty($val['img'])){
                            return ['flag'=>'error','msg'=>'电子胶片部位图片不能为空','url'=>''];
                            exit;
                            // return $model->error("电子胶片部位图片不能为空");
                        }
                    }

                    $form_data['patient_part_lat'] = json_encode($form_data['patient_part_lat']);
                    $form_data['electronic_report'] = json_encode($form_data['electronic_report']);
                    $form_data['electronic_film'] = json_encode($form_data['electronic_film']);

                }
                break;
            case "del_record":
                $type = 'del';
                if ($model->request->isPost()) {
                    $id = $form_data["id"];
                    Db::execute("update patient_case set is_deleted=3  where id=?", [ $id]);
                }
                break;
        }

        $result = true;
        if($model->request->isPost()){

            //$model 实例化之后的模型
            //$type 数据的操作类型
            //$form_data  form表单提交之后的表单
            //$url 跳转路径
            //$old_data 原有的数据
            //$attach_arr 附属表单数组
            //$primary_key 基本表单的主键值id
            $result = Base::operaTable($model,$userId,$type,$form_data,$controller.'/'.$list_action,$old_data,$model->attach_arr,'id',$admin_type);

        }else if($type == 'detail'){
            $result =  Base::operaTable($model,$userId,$type,$form_data,'',$old_data,$model->attach_arr,'id',$admin_type,false);
        }


        return $result;
    }


    public static function sendMsgToPatient($record_id){
            //获取当前数据操作问题
        $model = new Patientcase();
        $sendMsgName = Base::getOneFieldByTablesToSendMsg($record_id,$model->base_arr,$model->senMsgAttach);
       // print_r($sendMsgName);
        return $sendMsgName;
    }

}
